<?php
/**
 * amer站的春假活动页面接口文件
 * @auth huangcb
 * */

define('INTERFACE_CODE', md5('amer-cj-20120315'));
define('DB_TABLE_PRE','amer_cj_');//表前缀
//note 接口合法性验证
if( $_GET['code'] != INTERFACE_CODE ) exit('error code!');

date_default_timezone_set('EST');

require_once '../../conf/conf.php';
require_once '../ws_qrj/lib/Mysql.class.php';


//note 检查某IP是否进行过抽奖
function check_ip( $db ){
	$ip = $_POST['ip'];
	$ret = $db->getOne('surprise', "ip='{$ip}'");
	
	if( empty( $ret ) ){
		$db->insert('surprise',array('ip'=>$ip));
		echo 0;
	}else{
		$db->update('surprise',array('num'=>$ret['num']+1), "ip='{$ip}'");
		echo 1;		
	}
}


//note 设置中奖值
function set_prize( $db ){
	$ip = $_POST['ip'];
	$prize = $_POST['prize'];
	
	$db->update('surprise',array('prize'=>$prize), "ip='{$ip}'");
	$ret = $db->getOne('surprise',"ip='{$ip}'");
	echo $ret['id'];
}

//添加中奖用户
function add_prize_user( $db ){
	$cj_id = $_POST['cj_id'];
	$name = $_POST['name'];
	$email = $_POST['email'];
	$is_sub =$_POST['is_subscribe'];
	
	$db->update('surprise',array('name'=>$name,'email'=>$email,'is_sub'=>$is_sub), "id={$cj_id}");
}


//note 验证邮箱
function check_email( $db ){
	$email = $_GET['email'];
	$ret = $db->getOne('surprise',"prize = 5 AND email='{$email}'");
	
	if( empty( $ret ) ){
		echo 0;
	}else{
		echo 1;
	}
}


//note 发送抽奖地址邮件到用户邮箱
function mail_and_send( $db ){
	$email = $_POST['email'];
	$is_sub =$_POST['is_subscribe'];
	$ip = $_POST['ip'];
	
	echo $db->insert('check',array('email'=>$email,'is_sub'=>$is_sub,'ip'=>$ip));
}


function remind_me( $db ){
	$email = $_POST['email'];
	$is_sub =$_POST['is_subscribe'];
	$ip = $_POST['ip'];
	$os = $_POST['os'];
	$site = $_POST['site'];
	
	echo $db->insert('remind',array('email'=>$email,'is_sub'=>$is_sub,'ip'=>$ip,'os'=>$os,'site'=>$site));
}

//note 取得产品销售数字
function get_sale_num( $db ){
	$d = $db->getAll('sale',1);
	foreach( $d as $v ){
		$arr[ $v['site'].'-'.$v['os'].'-'.$v['pindex'] ] = $v['num'];
	}
	echo json_encode( $arr );
}

//note 设置产品销售数字
function set_sale_num( $db ){
	$site = $_POST['site'];
	$os = $_POST['os'];
	$pindex = $_POST['pindex'];
	
	$sql = "UPDATE ".DB_TABLE_PRE."sale SET num = num + 1 WHERE site = '{$site}' AND os = '{$os}' AND pindex = {$pindex}";
	$db->query( $sql );
}



//note 验证用户邮箱,抽奖
function check_mail( $db ){
	$check_id = $_GET['check_id'];
	$email = $_GET['email'];
	
	$ret = $db->getOne('check', "id={$check_id} AND email='{$email}'");
	
	if( empty( $ret ) ){
		echo 0;
	}else{
		if( $ret['is_check'] == 1 ){
			echo 1;
		}else{
			$db->update('check',array('is_check'=>1,'date_2'=>date('Y-m-d H:i:s')), "id={$check_id} AND email='{$email}'" );
			echo 2;
		}
	}
}



$db = new Mysql();
$m = $_GET['m'];
$m( $db );